{% extends "layout.html" %}
{% set active_page = "dependencies" %}
{% set help_page = ["https://kizniche.github.io/Mycodo/Dependencies/", _('Dependencies')] %}

{% block title %} - {{_('Dependencies')}}{% endblock %}

{% block head %}
  {% if install_in_progress == 1 %}
<script>
  let prev_string = '';
  function check_dependency_status() {
    const url = '/admin/dependency_status';
    $.ajax(url, {
      success: function(new_string, responseText, jqXHR) {
        if (jqXHR.status !== 204) {
          if (new_string !== prev_string) {
            prev_string = new_string;
            document.getElementById("dependency_status").innerHTML = new_string;
            let objDiv = document.getElementById("dependency_status");
            objDiv.scrollTop = objDiv.scrollHeight;
          }
        }
        else {
          document.getElementById("dependency_status").innerHTML = "Attempting to read the dependency log. Please wait...";
        }
      },
      cache: false
    });
  }
  check_dependency_status();
  setInterval(function() {
      check_dependency_status();
    }, 5000
  );
</script>
  {% endif %}
{% endblock %}

{%- block body %}
  <!-- Route: /dependencies -->
  <div class="container" style="padding-bottom: 1.7em">
    {% include 'flash_messages.html' %}

    <h4>{{_('Dependencies')}} <a href="{{help_page[0]}}" target="_blank"><span style="font-size: 16px" class="fas fa-question-circle"></span></a></h4>
    <p>{{_('Several software dependencies used by Mycodo features may be disabled. This is done to speed up the initial installation process. This page can be used to determine which dependencies are installed and the ability to install currently unmet dependencies.')}}</p>

  {% if device != '0' %}
    <h2>Device: {{device_name}} ({{device}})</h2>

    {% if device_unmet_dependencies %}

      {% if install_in_progress == 1 %}

    <p>The install has been initiated for the following dependencies:</p>
    <ul>
    {% for each_dep in device_unmet_dependencies %}
      <li>{% if each_dep[2] == 'internal' %}{{each_dep[3]}}{% else %}{{each_dep[0]}}{% endif %}</li>
    {% endfor %}
    </ul>
    <p>The Dependency Install Log below will refresh every 5 seconds. Wait until all dependencies have been installed to attempt to add the associated device again. You will know the entire install process have completed by "#### Dependency install finished" appearing at the end of the log.</p>
    <p>If there's an issue with the install and you need to reset the install state, delete /opt/Mycodo/.dependency to restore dependency install functionality.</p>

    <pre style="overflow:auto; resize: vertical; height:400px" id="dependency_status"></pre>

      {% else %}

    <p>There were unmet dependencies encountered when adding the following device: {{device}}</p>
    <p>Unmet dependencies:</p>
    <ul>
    {% for each_dep in device_unmet_dependencies %}
      <li>
        {%- if each_dep[2] == 'internal' -%}
          {{each_dep[3]}}
        {%- else -%}
          {{each_dep[0]}}
        {%- endif -%}
      </li>
    {% endfor %}
    </ul>

    {% if dependencies_message %}
    <p>Message about dependencies: {{dependencies_message}}</p>
    {% endif %}

    <p>To install these dependencies, click the Install button below and wait for the install to complete before attempting to add the device again. Note that the frontend and the backend daemon will be restarted following install of the dependencies to allow them to detect the newly-installed modules. If you do not wish the daemon to restart, postpone installing dependencies until your system is at a point where this can occur.</p>

    <form method="post" action="/admin/dependencies/{{device}}">
      {{form_dependencies.csrf_token}}
      {{form_dependencies.device(class_='form-control', value=device)}}
      <div class="row">
        <div class="col-12 col-sm-6 col-md-3">
          {{form_dependencies.install(class_='btn btn-primary')}}
        </div>
      </div>
    </form>

      {% endif %}

    {% else %}
    <p>All dependencies have been installed for this device.</p>
    {% endif %}

    <h4 style="padding: 1em 0 0 0.5em"><a href="/admin/dependencies"><i class="fa fa-arrow-left"></i> Return to device list</a></h4>

  {% else %}

     {% if unmet_list %}
    <div>
      <h3 style="color: #f78900">Some dependencies are not currently met</h3>
      <p>The following list contains dependencies that are not currently installed and the devices that use them. If you require the use of a feature that has one or more unmet dependencies, you must install them before being able to use the feature. Select a device name to view information about its depenencies and the option to install them.</p>

    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>Dependency</th>
          <th>Mycodo Feature(s)</th>
        </tr>
      </thead>
      <tbody>

      {% for each_dep, devices in unmet_list.items()|sort(case_sensitive=False) %}
        <tr>
          <th scope="row">{{loop.index}}</th>
          <td>
            {%- if each_dep[2] == 'internal'-%}
              {{each_dep[3]}}
            {%- else -%}
              {{each_dep[0]}}
            {%- endif -%}
          </td>
          <td>
        {% for each_device in devices %}<a href="{{each_device}}">{{each_device}}</a>{% if not loop.last %}, {% endif %}{% endfor %}
          </td>
        </tr>
      {% endfor %}

      </tbody>
    </table>

    </div>
    {% endif %}

    {% if unmet_exist %}
    <div style="padding-top:1.5em">
      <h4>List of devices with unmet dependencies</h4>
      {% for each_device, dependencies in unmet_dependencies.items()|sort(case_sensitive=False) if dependencies %}
      <div>
        <a href="{{each_device}}">{{each_device}}</a>: {% for each_dep in dependencies %}{% if each_dep[2] == 'internal' %}{{each_dep[3]}}{% else %}{{each_dep[0]}}{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}
      </div>
      {% endfor %}
    </div>
    {% else %}
      <h3 style="color: #4E9258">All dependencies are currently installed</h3>
    {% endif %}

    {% if met_exist %}
    <div style="padding-top:1.5em">
      <h4>List of devices that have all dependencies installed</h4>
      {% for each_device in met_dependencies|sort(case_sensitive=False) %}
      <div>
        <a href="{{each_device}}">{{each_device}}</a>
      </div>
      {% endfor %}
    </div>
    {% endif %}

  {% endif %}

  </div>

{% endblock -%}
